// pages/newStudent/newStudent.js
var app = getApp();
var ea = require('../../utils/each.js');
var time = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getxueshengUrl: app.data.url + 'zhongxue/teacher/gettongzhixuesheng',
    getbanjiUrl: app.data.url + 'zhongxue/teacher/getbanji',
    shuju: {},
    length: '',
    banji: '',
    items: [
      {
        code: 1,
        url: "/img/23.png",
        name: "陈雪",
        gender: "女",
        age: "12岁"
      },
      {
        code: 2,
        url: "/img/25.png",
        name: "陈雪",
        gender: "女",
        age: "12岁"
      },
      {
        code: 3,
        url: "/img/23.png",
        name: "陈雪",
        gender: "女",
        age: "12岁"
      },
      {
        code: 4,
        url: "/img/25.png",
        name: "陈雪",
        gender: "女",
        age: "12岁"
      },
      {
        code: 5,
        url: "/img/23.png",
        name: "陈雪",
        gender: "女",
        age: "12岁"
      },
      {
        code: 6,
        url: "/img/25.png",
        name: "陈雪",
        gender: "女",
        age: "12岁"
      },
      {
        code: 7,
        url: "/img/23.png",
        name: "陈雪",
        gender: "女",
        age: "12岁"
      },

    ],
    select_all: false,
    batchIds: '',
  },
  skip: function () {
    var studentsid = this.data.batchIds;
    wx.setStorageSync('tongzhi', studentsid);
    wx.navigateTo({
      url: '/pages/inform/inform',
    })
  },

  xuanzhe: function (e) {
    var array = e.detail.value;
    var shuju = this.data.shuju;
    var batchIds = [];
    ea.each(shuju, function (i, v) {
      // console.log(v.id);
      // console.log(array)
      // console.log(array.indexOf(v.id.toString()));
      if (!(array.indexOf(v.id.toString()) == -1)) {
        batchIds.push(v['students_id']);
      }
    });
    console.log(batchIds);
    if (batchIds.length == shuju.length) {
      this.setData({ select_all: true });
    } else {
      this.setData({ select_all: false });
    }
    this.setData({ batchIds: batchIds });
  },

  quanxuan: function (e) {
    var that = this;
    // console.log(that)
    var arr = [];   //存放选中id的数组
    for (let i = 0; i < that.data.shuju.length; i++) {

      that.data.shuju[i].checked = (!that.data.select_all)

      if (that.data.shuju[i].checked == true) {
        // 全选获取选中的值
        arr = arr.concat(that.data.shuju[i]['students_id']);
      }
    }
    console.log(arr);
    that.setData({
      shuju: that.data.shuju,
      select_all: (!that.data.select_all),
      batchIds: arr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var user = wx.getStorageSync('newuser');
    // console.log(user)
    var id = options.id;
    var token = user.token;
    var shenhe = user.shenhe;
    var shuju = that.data.shuju;
    var lenth = that.data.length;
    wx.request({
      url: that.data.getxueshengUrl,
      method: 'post',
      data: { token: token, shenhe: shenhe, id: id },
      success: function (res) {
        var data = res.data
        console.log(data);
        if (data.code == 200) {
          var shuju = data.data;
          // console.log(shuju)
          that.setData({ shuju: shuju });
          var lenth = shuju.length;
          that.setData({ length: lenth });
          // console.log(that)
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'none',
          })
        }
      }
    })
    var banji = that.data.banji;
    wx.request({
      url: that.data.getbanjiUrl,
      method: 'post',
      data: { id: id },
      success: function (res) {
        // console.log(res);
        var data = res.data;
        // console.log(data)
        if (data.code == 200) {
          var banji = data.data.bian;
          // console.log(banji)
          that.setData({ banji: banji });
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'none',
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})